package com.svmc.app.task.activity;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import luyenlv.pro.screen.base.BaseActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.EditText;

import com.svmc.app.task.tabmanager.TabGroupActivity;
import com.svmc.app.task.utils.CommonDefine;
import com.svmc.app.task.view.CustomDialog;

public class FeedBack extends BaseActivity implements OnClickListener,CommonDefine{
	EditText title;
	EditText bodyMail;
	ProgressDialog dialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(getLayout(R.array.feedback));
		
		title =(EditText)findViewById(R.id.feedback_edt_title);
		bodyMail =(EditText)findViewById(R.id.feedback_edit_body);
		findViewById(R.id.feedback_img_back).setOnClickListener(this);
		findViewById(R.id.feedback_img_done).setOnClickListener(this);
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
		TabGroupActivity.stateActivity=FEEDBACK;
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.feedback_img_back:
			finish();
			break;
		case R.id.feedback_img_done:
//			sendEmail("anh son", "anh ho");
			new SendFeedback().execute();
			break;
		default:
			break;
		}
		
	}
	public boolean sendEmail (String title,String body){
		final String username = EMAIL_FROM; // user name
		final String password = PASS_EMAIL;// pass

		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "587");

		Session session = Session.getInstance(props,
				new javax.mail.Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(username, password);
					}
				});

		try {

			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress(EMAIL_FROM));
			message.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(EMAIL_TO));
			message.setSubject(title);
			message.setText(body);
			// message.setText("Dear Mail Crawler,"
			// + "\n\n No spam to my email, please!");

			Transport.send(message);

			System.out.println("Done");
			return true;

		} catch (MessagingException e) {
			//throw new RuntimeException(e);
			System.out.println("Khong the ket noi");
			return false;
		}
			
	}
	public class SendFeedback extends AsyncTask<Void, Void, Void>{
		
		CustomDialog mydialog = new CustomDialog(getDialogContext());
		boolean result_email;
		@Override
		protected Void doInBackground(Void... params) {
			result_email=sendEmail(title.getText().toString(), bodyMail.getText().toString());
			
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			dialog.dismiss();
			if(result_email==true){
				FeedBack.this.finish();
			}else if (result_email==false) {
				mydialog.setDialogType(EMAIl_SEND_FAIL, FeedBack.this.getString(R.string.feedback_email_result_fail));
				mydialog.show();
			}
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dialog=new ProgressDialog(getDialogContext());
			dialog.setMessage("Sending...");
			dialog.show();
		}
		
		
		
		
		
	}
	/**
	 *  Get context in groupActivity.Use for show dialog
	 * @return : context
	 */
	private Context getDialogContext() {
	    Context mcontext;
	    if (getParent() != null) mcontext = getParent();
	    else mcontext = this;
	    return mcontext;
	}

	@Override
	public void clearMemory() {
		// TODO Auto-generated method stub
		
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if(dialog !=null&&dialog.isShowing()){
			dialog.cancel();
		}
	}
	
	
	
}

